# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=parsec
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.0.2209
pkgrel=1
pkgdesc='PaRSEC: Parallel Runtime Scheduling and Execution Controller (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
depends=()
makedepends=("${MINGW_PACKAGE_PREFIX}-hwloc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
license=('custom')
url='https://icl.utk.edu/parsec/'
source=("https://github.com/ICLDisco/parsec/archive/refs/tags/parsec-${pkgver}.tar.gz")
sha256sums=('67d383d076991484cb2a265f56420abdea7cc1f329c63ac65a3e96fbfb6cc295')

prepare() {
    cd ${srcdir}
    rm -rf ${_realname}-${pkgver}.tar.gz
    cp -r * ${_realname}-${pkgver}
}

build() {
  #Static Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  # for asprintf
  CFLAGS+=" -D_GNU_SOURCE"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
    -G'Ninja' \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DCUDA_USE_STATIC_CUDA_RUNTIME=OFF \
    -DPARSEC_GPU_WITH_CUDA=OFF \
    -DPARSEC_WITH_DEVEL_HEADERS=ON \
    -DBUILD_SHARED_LIBS=OFF \
    -DPARSEC_DIST_WITH_MPI=OFF \
    -DBUILD_TESTING=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

package() {
  #Static Install
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR=${pkgdir} cmake --build ./ --target install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/License.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  # Remove msys2 full path reference
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/share/cmake/parsec/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i "${_f}"
  done

  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${_f}"
  done
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/include/parsec/parsec_config.h
}
